package base

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"io"
	"net/http"
)

func Sign(req *http.Request, body []byte, secretKey string) string {

	h := hmac.New(sha1.New, []byte(secretKey))

	data := req.URL.Path
	if req.URL.RawQuery != "" {
		data += "?" + req.URL.RawQuery
	}

	io.WriteString(h, data+"\n")

	if len(body) > 0 {
		h.Write(body)
	}

	return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
